//
//  XiangCeViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/4.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "XiangCeViewController.h"

@interface XiangCeViewController ()

@end

@implementation XiangCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [kBlackColor colorWithAlphaComponent:0.3];
    self.imageViewContent.image = _image;
}


- (IBAction)backButtonClick:(UIButton *)sender {
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}


//点击使用，在这个页面进行二维码识别，并将内容发送到扫一扫页面
- (IBAction)useButtonClick:(UIButton *)sender {
    [self scanQRCode:_image];
}


- (void)scanQRCode:(UIImage *)image {
    
    CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
    CIImage *img = [CIImage imageWithCGImage:image.CGImage];
    NSArray *features = [detector featuresInImage:img];
    
    if (features.count > 0) {
        CIQRCodeFeature *feature = [features firstObject];
        NSString *scannedResult = feature.messageString;
        NSLog(@"result:%@",scannedResult);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"qrcode" object:scannedResult];
    }else{
        [self alertVCByTitle:@"未能识别出二维码"];
    }
}


- (void)alertVCByTitle:(NSString *)title{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
